package com.jeasonzhao.commons.json;

import com.jeasonzhao.commons.parser.lex.LexToken;
import com.jeasonzhao.commons.parser.lex.LexTokenCollection;
import com.jeasonzhao.commons.utils.Algorithms;

public class JSONConstant extends JSONNode
{
    private static final long serialVersionUID = 1L;
    private static final int MAX_NUMBER = 16;
    private LexToken m_token = null;
    public JSONConstant()
    {
        super(JSONNodeTypes.CONSTANT);
    }

    public void setValue(Object value)
    {
        if(null == value)
        {
            return;
        }
        else if(value instanceof JSONConstant)
        {
            this.m_token = ((JSONConstant) value).m_token;
        }
        else if(value instanceof LexToken)
        {
            this.m_token = (LexToken) value;
        }
        else
        {
            String key = Algorithms.toString(value);
            //is it necessary using string cast??
//            DataTypes dt= DataTypes.from(value.getClass());
//            if(dt.isNumeric())
//            {
//            }
            this.m_token = LexToken.create(key);
        }
        if(null != m_token)
        {
            this.m_tokens = new LexTokenCollection();
            m_tokens.add(m_token);
        }

    }

    public JSONConstant(Object value)
    {
        super(JSONNodeTypes.CONSTANT);
        setValue(value);
    }

    public String getValue()
    {
        return null == m_token ? null : m_token.getToken();
    }

    public String toJSON(boolean isFullMode,int nLevel)
    {
        if(null == m_token
           || m_token.isComment()
           || null == m_token.getToken())
        {
            return "";
        }
        else if(m_token.isString())
        {
            String str = m_token.getToken();
            if(this.javaScriptCompatible)
            {
                if(str.equals("true")
                   || str.equals("false"))
                {
                    return str;
                }
                else if(java.util.regex.Pattern.matches("^[+-]{0,1}[\\d.]+$",str)&& str.length() <= MAX_NUMBER)
                {
                    if(str.startsWith("0."))
                    {
                        return str;
                    }
                    else if(str.length() > 1 && str.startsWith("0"))
                    {
                        //No code here
                    }
                    else
                    {
                        return str;
                    }
                }

            }
            str = str
                .replaceAll("\\\\","\\\\\\\\")
                .replaceAll("\"","\\\\\"")
                .replaceAll("\\n","\\\\n")
                .replaceAll("\\t","\\\\t")
                .replaceAll("\\r","\\\\r");
            return "\"" + str + "\"";
        }
        else
        {
            return m_token.getToken();
        }
    }

    public static void main(String[] argvs)
    {
        System.out.println(new JSONConstant("10051001100110011002").toJSON());
        System.out.println(new JSONConstant("01").toJSON());
        System.out.println(new JSONConstant("0.1").toJSON());
        System.out.println(new JSONConstant("0").toJSON());
        System.out.println(new JSONConstant("1111111111111").toJSON());
        System.out.println(new JSONConstant("2").toJSON());
    }
}
